/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tweetbotserver.server.comms;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tweetbotserver.server.message.MessageProcessor;
import tweetbotserver.server.message.TwitterMessageProcessor;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/**
 *
 * @author Mike
 */
public class TwitterCommunicationsManager {

    private MessageProcessor messageProcessor = null;
    private List<TwitterCommunicationsListener> twitterCommunicationsListeners = null;
    private String username = null;
    private String password = null;
    private Twitter twitterClient = null;

    public TwitterCommunicationsManager(String twitterUsername, String twitterPassword) {
        System.out.println("Created TwitterCommunicationsManager");
        username = twitterUsername;
        password = twitterPassword;
        twitterClient = new Twitter(username,password);
        messageProcessor = new TwitterMessageProcessor();
        twitterCommunicationsListeners = new ArrayList<TwitterCommunicationsListener>();
    }

    private void informListeners(String message) {
        for (TwitterCommunicationsListener listener : twitterCommunicationsListeners) {
            listener.handleTwitterMessage(message);
        }

    }

    public void addTwitterCommunicationsListener(TwitterCommunicationsListener listener) {
        twitterCommunicationsListeners.add(listener);
    }

    public void start() {
        // poll for status updates for user
        // when we get an update pass to our listeners

        String twitterMessage = "@FrozenKnight hello twitter";

        //informListeners(twitterMessage);




    }

    public void tweet(String message) {
        System.out.println("Tweeting message:" + message);

        Status status;
        try {
            status = twitterClient.updateStatus(message);
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
        } catch (TwitterException ex) {
            Logger.getLogger(TwitterCommunicationsManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
